##########################################
## Copyright 2012-2013 Ceruti Francesco & contributors
##
## This file is part of LiSP (Linux Show Player).
##########################################

from PyQt4 import QtCore
from uuid import uuid4

from .timing import MediaTime


class Media(QtCore.QObject):
    '''
    An Abstract base for all the media on the software system.
    The "emit" parameter is used for decide when a signal should be emit.
    '''

    '''
    Media status: describe the current media state.
    Can be different from the effective playback state.
    '''
    PLAYING = 3  # Media in play
    PAUSED = 2  # Media in pause
    STOPPED = 1  # Media ready for playback
    NONE = 0  # Media not configured

    '''
    The MEDIA_DEFAULT_CONF provide a set of configuration parameters needed for use a media properly.
    For add parameters on subclasses use: Media.MEDIA_CONF.update(dict).
    NOTE: __init__() add "uuid" to Media.conf, this property is not in MEDIA_DEFAULT_CONF because
        no default value is needed.
    '''
    MEDIA_DEFAULT_CONF = {'name': 'Empty Media',
                          'location': '',
                          'groups': [],
                          'stopAll': False}

    '''
    PyQt-provided signals:
        for thread-safe usage   -> signal.connect(callable, QtCore.Qt.QueuedConnection)
        for normal usage        -> signal.connect(callable)
        for info see PyQt documentation
    '''
    onPlay = QtCore.pyqtSignal(object)                  # Media go in play state
    onStop = QtCore.pyqtSignal(object)                  # Media go in stop state
    onStopped = QtCore.pyqtSignal(object)               # Media playback is stopped
    onPause = QtCore.pyqtSignal(object)                 # Media go in pause state
    onPaused = QtCore.pyqtSignal(object)                # Media playback is paused
    onSeek = QtCore.pyqtSignal(object, int)             # Media is sought
    onEndOfStream = QtCore.pyqtSignal(object)           # Media stream ended
    onError = QtCore.pyqtSignal(object, str)            # Media error
    onDuration = QtCore.pyqtSignal(object, int)         # Media duration changed
    onFadeOut = QtCore.pyqtSignal(bool)         # Media enter/exit fade-out
    onFadeIn = QtCore.pyqtSignal(bool)          # Media enter/exit fade-in
    onConfigurationChanged = QtCore.pyqtSignal(object)  # Media configuration changed

    state = NONE  # current media state

    def __init__(self, uuid=None):
        super(Media, self).__init__()

        self.conf = {}              # current media configuration. DON'T OVVERIDE, use only dict.update(dict)
        self.mediaTime = MediaTime(self, wait=0.1)

        if(uuid is None):
            self.uuid = str(uuid4())    # DON'T CHANGE
        else:
            self.uuid = str(uuid)

    def currentTime(self):
        '''Get the current playback time, or -1 for error'''

    def interrupt(self, reconf=True):
        '''Stop the media without notification or effects'''

    def pause(self, emit=True):
        '''Go in pause state'''

    def play(self, emit=True):
        '''Go in play state'''

    def seek(self, seek_time, emit=True):
        '''Seek to time (unit depends from extension)'''

    def updateConfiguration(self, conf, emit=True, duration=True):
        '''Update the configuration'''

    def stop(self, emit=True):
        '''Go in stop state'''
